package com.kehutong.material.app;

import java.util.List;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import com.kehutong.common.util.Token;
import com.kehutong.material.entity.Question;
import com.kehutong.material.service.impl.QuestionServiceImpl;

/**
 * 问题库
 * @author: liukunlong
 * @date: 2021-03-06 14:04
 */
@MvcAction
@ReqMapping("/kht-bin/question")
public class QuestionAction {

    @Inject
    private QuestionServiceImpl questionService;
    @Inject
    private JdbcSession jdbcSession;
    /**
     * 问题库列表
     * @param token 用户信息
     * @param jsonObject 参数对象
     * @return 问题列表
     */
    @ReqMapping("/listQuestion")
    public Object listQuestion(Token token, JSONObject jsonObject) throws Exception{
        //todo 需要企业微信前段测试是否符合预期
        List<Question> questionList=questionService.getQuestionList(jsonObject).getList();
        return Result.success(questionList);
    }

    @ReqMapping("/list")
    public Object fileList(Token token, JSONObject jsonObject){
        List<Question> folderList = jdbcSession.findArray(Question.class)
                                               .eq(jsonObject, "pid")
                                               .like(jsonObject, "name")
                                               .eq("isFolder", false)
                                               .eq("deleted", false)
                                               .exe();
        return Result.success(folderList);
    }

    @ReqMapping("/folder/list")
    public Object folderList(JSONObject jsonObject){
        List<Question> folderList = jdbcSession.findArray(Question.class)
                                               .eq(jsonObject, "pid")
                                               .like(jsonObject, "name")
                                               .eq("isFolder", true)
                                               .eq("deleted", false)
                                               .exe();
        return Result.success(folderList);
    }

    @ReqMapping("/search")
    public Object search(JSONObject jsonObject){
        List<Question> folderList = jdbcSession.findArray(Question.class)
                                               .eq(jsonObject, "pid")
                                               .like(jsonObject, "name")
                                               .eq("isFolder", false)
                                               .eq("deleted", false)
                                               .exe();
        return Result.success(folderList);
    }

    //todo 返回数据是否需要进行转换需要测试
    JSONObject toJson(Question question) {
        final JSONObject jsonObject = new JSONObject();

        jsonObject.put("id", question.getId());
        jsonObject.put("name", question.getName());
        jsonObject.put("answer", question.getAnswer());
        jsonObject.put("type", "2");

        return jsonObject;
    }
}
